
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "commtraff.h"
#include "commtraff_napi.h"

namespace OHOS {
namespace KhCommtraff {

    void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
    {
        napi_value prop = nullptr;
        if (napi_create_int32(env, objName, &prop) == napi_ok) {
            napi_set_named_property(env, dstObj, propName, prop);
        }
    }

    napi_value DefineCommtraffEnums(napi_env env, napi_value exports) {
        // 错误码枚举
        napi_value CommTraffErrCode = nullptr;
        napi_create_object(env, &CommTraffErrCode);

        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_SUCCESS), "COMMTRAFF_SUCCESS");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_FAIL), "COMMTRAFF_FAIL");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_INIT_FAIL), "COMMTRAFF_INIT_FAIL");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_INVALID_PARAM), "COMMTRAFF_INVALID_PARAM");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_MEM_ALLOC_FAIL), "COMMTRAFF_MEM_ALLOC_FAIL");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_NO_RRD_FILE), "COMMTRAFF_NO_RRD_FILE");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_NO_PERMISSION), "COMMTRAFF_NO_PERMISSION");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_OPEN_DIR_FAIL), "COMMTRAFF_OPEN_DIR_FAIL");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_NULL_PTR), "COMMTRAFF_NULL_PTR");
        SetNamedPropertyByInteger(env, CommTraffErrCode, static_cast<int>(CommTraffErrCode::COMMTRAFF_UV_WORK_FAIL), "COMMTRAFF_UV_WORK_FAIL");
        napi_set_named_property(env, exports, "CommTraffErrCode", CommTraffErrCode);

        // 流量类型枚举
        napi_value TraffType;
        napi_create_object(env, &TraffType);
        const char* traffTypeNames[] = {
                "LINK_LAYER_DATA", "TRANSPORT_LAYER_DATA",
                "SESSION_LAYER_DATA", "RPC_LAYER_DATA",
                "WPA_LAYER_DATA", "HISYSEVENT_DATA"
        };
        for (int i = LINK_LAYER_DATA; i <= HISYSEVENT_DATA; ++i) {
            SetNamedPropertyByInteger(env, TraffType, i, traffTypeNames[i]);
        }
        napi_set_named_property(env, exports, "TraffType", TraffType);

        // 会话类型枚举
        napi_value SessionType;
        napi_create_object(env, &SessionType);
        const char* sessionTypeNames[] = {"INNER_SESSION", "BYTE_SESSION", "MESSAGE_SESSION", "STREAM_SESSION", "FILE_SESSION"};
        for (int i = INNER_SESSION; i < FILE_SESSION; ++i) {
            SetNamedPropertyByInteger(env, SessionType, i, sessionTypeNames[i]);
        }
        napi_set_named_property(env, exports, "SessionType", SessionType);

        return exports;
    }

static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
            DECLARE_NAPI_STATIC_FUNCTION("CommtraffInit", CommtraffInitJs),
            DECLARE_NAPI_STATIC_FUNCTION("CommtraffDeinit", CommtraffDeinitJs),
            DECLARE_NAPI_STATIC_FUNCTION("InsertCommTraff", InsertCommTraffJs),
            DECLARE_NAPI_STATIC_FUNCTION("QueryCommTraff", QueryCommTraffJs),
            DECLARE_NAPI_STATIC_FUNCTION("QueryCommTraffNums", QueryCommTraffNumsJs),

            DECLARE_NAPI_STATIC_FUNCTION("On", JsOn),
            DECLARE_NAPI_STATIC_FUNCTION("Off", JsOff),

    };
    NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc));
    DefineCommtraffEnums(env, exports);
    return exports;
}
} // namespace KhCommtraff
} // namespace OHOS

static napi_module g_module = {
        .nm_version = 1,
        .nm_flags = 0,
        .nm_filename = nullptr,
        .nm_register_func = OHOS::KhCommtraff::Init,
        .nm_modname = "khCommtraff",
        .nm_priv = ((void*)0),
        .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterModule() {
    napi_module_register(&g_module);
}